﻿using Dapper;
using MediatR;
using MySql.Data.MySqlClient;
using MySqlX.XDevAPI.Common;
using System.Text;
using Warehouse.Api.Read.AGCation.Command;
using Warehouse.Api.Read.Dtos;
using Warehouse.Domian.Model;
using Warehouse.ErrorCode;
using Warehouse.Infrastructrue.Implenment;

namespace Warehouse.Api.Read.AGCation.Hanlder
{
    public class BasicinformatioCommandHanlder : IRequestHandler<BasicinformatioCommand, ApiPaging<BasicinformatioDto>>
    {
        DbContext db;
        ILogger<BasicinformatioCommandHanlder> logger;

        public BasicinformatioCommandHanlder(DbContext db, ILogger<BasicinformatioCommandHanlder> logger)
        {
            this.db = db;
            this.logger = logger;
        }
        /// <summary>
        /// 获取库存信息列表(添加)
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async Task<ApiPaging<BasicinformatioDto>> Handle(BasicinformatioCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<BasicinformatioDto> result = new ApiPaging<BasicinformatioDto>();
            try
            {
                StringBuilder sqlbuilder = new StringBuilder();
                StringBuilder countsql = new StringBuilder();

                sqlbuilder.Append(@" SELECT 
                                       inven.TrackingNumber,
                                       inven.WarehouseName,
                                       inven.StartTime,
                                       inven.EndTime,
                                       inven.Satate, 
                                       inven.InventoryName,
                                       inven.Inventorycount,
                                       inven.States,
                                       good.GoodName,
                                       good.Specification,
                                       good.Inventory,
                                       type.TypeName,
                                      res.ResultName,
                                     inven.Inventorytota
                             FROM inventorymodel inven 
JOIN warehousetypemodel type ON inven.TypeId = type.TypeId 
JOIN resultmodel res ON inven.ResultId = res.ResultId 
JOIN  GoodsType good ON inven.GoodId = good.GoodId
WHERE 0= 0 ");
                countsql.Append(@" SELECT COUNT(inven.InventiryId) FROM inventorymodel inven 
                             JOIN warehousetypemodel type ON inven.TypeId = type.TypeId 
                             JOIN resultmodel res ON inven.ResultId = res.ResultId 
                             JOIN  GoodsType good ON inven.GoodId = good.GoodId  WHERE 0= 0; ");
                var parmenters = new DynamicParameters();
                sqlbuilder.Append(" LIMIT @pagesize offset @offset");
                parmenters.Add("pagesize", request.PageSize);
                parmenters.Add("offset", (request.PageIndex - 1) * request.PageSize);
                using var conn = new MySqlConnection("server=120.27.210.76;uid=wms_user;pwd=MyPassword123!;database=warehousemanagement;CharSet=utf8mb4;");
                await conn.OpenAsync(cancellationToken);
                var data = await conn.QueryAsync<BasicinformatioDto>(sqlbuilder.ToString(), parmenters);
                var total = await conn.ExecuteScalarAsync<int>(countsql.ToString(), parmenters);
                if (request.PageSize <= 0)
                {
                    result.Msg = "显示数量不能小于等于0";
                    result.Code = ApiEume.Error;
                    return result;
                }
                result.Msg = "显示成功";
                result.Code = ApiEume.Success;
                result.PageData = data.ToList();
                result.TotaCount = total;
                result.PageCount = (int)Math.Ceiling((double)total / request.PageSize);
            }
            catch (Exception ex)
            {

            }
            return result;
        }
    }
}
